/**
 * Port of an example found on the SQLite site.
 * http://www.sqlite.org/quickstart.html
 */
using GLib;
using Sqlite;

public class Demo : Object
{
    [NoArrayLength ()]
    public static int callback(pointer data, int n_columns, string[] values, string[] column_names) {
        for (int i = 0; i < n_columns; i++) {
            stdout.printf("%s = %s\n", column_names[i], values[i]);
        }
        stdout.printf("\n");

        return 0;
    }

    static int main (string[] args) {
        Database db;
        string errmsg;
        int rc;

        if ( args.length != 3 ) {
            stderr.printf("Usage: %s DATABASE SQL-STATEMENT\n", args[0]);
            return 1;
        }

        rc = Database.open(args[1], ref db);

        if ( rc == 0 ) { // SQLITE_OK -> Sqlite.OK == 0
            stderr.printf("Can't open database: %s\n", db.errmsg);
            return 1;
        }

        rc = db.exec(args[2], callback, null, ref errmsg);

        if ( rc != 0 ) { // SQLITE_OK -> Sqlite.OK == 0
            stderr.printf("SQL error: %s\n", errmsg);
        }

        return 0;
    }
}


